package com.tiancheng.trade.payment.mq.producer;

import com.tiancheng.trade.commom.core.data.MessageBody;
import com.tiancheng.trade.commom.core.enums.TradeRabBitMqEnums;
import com.tiancheng.trade.commom.core.utils.JsonUtil;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.AmqpException;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.stereotype.Component;

/**
 * @author jiangjun
 * @description 推送订单中心结算消息发送者
 * @date 2024/12/29 23:39
 */
@Component
@Slf4j
public class PaymentSettle2OrderMsgProducer implements IRabbitProducer {

    @Resource
    private AmqpTemplate amqpTemplate;

    @Override
    public void sendMessage(MessageBody messageBody, long expiration) {
        //给延迟队列发送消息
        amqpTemplate.convertAndSend(exchange().getExchange(), routingKey().getRouteKey(), JsonUtil.toJsonString(messageBody), new MessagePostProcessor() {
            @Override
            public Message postProcessMessage(Message message) throws AmqpException {
                //给消息设置延迟毫秒值
                message.getMessageProperties().setExpiration(String.valueOf(expiration));
                return message;
            }
        });
    }

    @Override
    public TradeRabBitMqEnums exchange() {
        return TradeRabBitMqEnums.QUEUE_SETTLE_MESSAGE;
    }

    @Override
    public TradeRabBitMqEnums routingKey() {
        return TradeRabBitMqEnums.QUEUE_SETTLE_MESSAGE;
    }
}
